﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SQLite;

using FMPCore;
using FMPCore.Interfaces;

namespace Server
{
    // NOTE: If you change the class name "Message" here, you must also update the reference to "Message" in App.config.
    [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
    public class Message : IMessage
    {
        private static Dictionary<string, IMessageCallback> users = new Dictionary<string, IMessageCallback>();

        /// <summary>
        /// Logs a user offf the server
        /// </summary>
        /// <param name="username">The username to logoff</param>
        public void Logout(string username)
        {
            UserDB udb = new UserDB();
            udb.LogoutUser(username);

            users.Remove(username);
        }

        /// <summary>
        /// Logs a user onto the server
        /// </summary>
        /// <param name="username">The username to login</param>
        /// <param name="password">The password for the user</param>
        /// <returns>True is sucessfull login, otherwise false for failed login</returns>
        public bool Login(string username, string password)
        {
            //TODO: Add checks for existing users  !users.Contains(callback)
            IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
            users.Add(username, callback);

            UserDB udb = new UserDB();
            return udb.LoginUser(username, password);
        }

        /// <summary>
        /// Check to see if the user Exist in the database
        /// </summary>
        /// <param name="username">The username to check</param>
        /// <returns>True if the usernaem exist, otherwise false</returns>
        public bool UserExist(string username)
        {
            UserDB udb = new UserDB();
            return udb.UserExist(username);
        }

        /// <summary>
        /// Sends a message to another users
        /// </summary>
        /// <param name="message">The message to send</param>
        public void SendMessage(ChatMessage message)
        {
            IMessageCallback callback = users[message.To];
            callback.RecieveMessage(message);
            System.Diagnostics.Trace.WriteLine(
                string.Format("{0} {1} sending message to {2}.", DateTime.Now, message.From, message.To)
            );
        }

        /// <summary>
        /// GEt a list of users that are on a user's contact list
        /// </summary>
        /// <param name="username">The user that wants it's contact list</param>
        /// <returns>List of contacts</returns>
        public List<Contact> GetContactList(string username)
        {
            ContactListDB cdb = new ContactListDB();
            return cdb.GetContactList(username);
        }

        /// <summary>
        /// Adds a contact to a user's contact list
        /// </summary>
        /// <param name="currentUser">The user that is adding a contact</param>
        /// <param name="userToAdd">The contact the user wants to add</param>
        /// <returns>True - if the the contact was added, false if it failed</returns>
        public bool AddContact(string currentUser, string userToAdd)
        {
            ContactListDB cdb = new ContactListDB();
            return cdb.AddContact(currentUser, userToAdd);
        }

        /// <summary>
        /// Adds a contact to a user's contact list
        /// </summary>
        /// <param name="currentUser">The user that is adding a contact</param>
        /// <param name="userToAdd">The contact the user wants to add</param>
        /// <returns>True - if the the contact was added, false if it failed</returns>
        public bool AddUser(string name, string password)
        {
            UserDB udb = new UserDB();
            return udb.addUser(name, password);
        }

        /// <summary>
        /// Removes a contact of the current users list
        /// </summary>
        /// <param name="currentUser">The current user</param>
        /// <param name="userToRemove">The user to remove form the current users list</param>
        /// <returns>True if the contact was remove sucessfully otherwise false</returns>
        public bool RemoveContact(string currentUser, string userToRemove)
        {
            ContactListDB cdb = new ContactListDB();
            return cdb.RemoveContact(currentUser, userToRemove);
        }

        public FileTransferMessageArgsResponse SendFileTransferRequestMessage(FileTransferMessageArgs msgFileTransferArgs)
        {
            IMessageCallback callback = users[msgFileTransferArgs.To];
            System.Diagnostics.Trace.WriteLine(
                string.Format("{0} {1} sending file transfer request message to {2}.", DateTime.Now, msgFileTransferArgs.From, msgFileTransferArgs.To)
            );
            return callback.RecieveFileTransferRequest(msgFileTransferArgs);
        }
    }
}
